import axios from 'axios'
import router from '@/router'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, 
  timeout: 5000 
})

//请求拦截器
service.interceptors.request.use(
  config => {
 const token=window.sessionStorage.getItem('token')

 if(token){
  config.headers['Authorization']=`Bearer ${token}`//后台要求Bearer加token 
 }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
//响应拦截器
service.interceptors.response.use(
  response => {

  //   const {message} = response.data
  //   console.log(message);
  //  if(message=='登录成功'){
    
  //  }else{

  //  }

  return response
  },
  error => {
        //逻辑失败
        console.dir(error.response);
        //token过期，清空token，然后返回登录页
        if(error.response.data.code=='10002'){
             window.sessionStorage.removeItem('token')
             router.replace('/login')
        }
    return Promise.reject(error)
  }
)

export default service
